<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="layui-container">
    <div class="layui-form">
        <button class="layui-btn layui-btn-normal addBtn" href-url=""><i class="layui-icon">&#xe61f;</i>添加新闻</button>
        <button class="layui-btn layui-btn-danger" id="deleteMany"><i class="layui-icon">&#xe640;</i>批量删除</button>
    </div>
    <table id="news" lay-filter="news"></table>
</div>
<script th:inline="javascript">
    layui.use(['table','element','form','layer'], function() {
        var table = layui.table,
            element = layui.element,
            form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //页面初始化
        form.render();

        /**
         * 商品表格
         */
        table.render({
            elem: '#news'
            , url: 'news/newsList' //数据接口
            , page: true //开启分页
            , limit: 5
            , limits: [5]
            , id: 'news'
            , cols: [
                [ //表头
                    {checkbox: true,width: '5%' ,fixed: 'left'}
                    ,{field: 'id', title: 'ID', width: '5%', sort: true}
                    , {field: 'title', title: '标题', width: '40%'}
                    , {field: 'createtime', title: '创建时间', width: '10%', sort: true,templet:'#createtime'}
                    , {field: 'updatetime', title: '更新时间', width: '10%', sort: true,templet:'#updatetime'}
                    , {fixed: 'right', title: '操作', toolbar: "#bar1", width: '30%'}
                ]
            ]
        });

        /**
         * 批量删除
         */
        $("#deleteMany").click(function () {
            //获取选中的行数据
            var checkData = table.checkStatus('news').data;
            //获取选中行的id装入数组
            var ids = new Array();
            $.each(checkData,function (idx,obj) {
                ids[idx] = obj.id;
            })
            if(ids.length>0){
                layer.confirm('真的删除行么', function(index) {
                    $.ajax({
                        type: "POST",
                        url: "news/deleteMany",
                        data: JSON.stringify(ids),
                        dataType: "json",
                        contentType : 'application/json;charset=utf-8',
                        success: function(data){
                            table.reload('news');
                            layer.msg(data.msg);
                        }
                    });
                })
            }else{
                layer.msg("尚未选中任何数据行");
            }
        })

        /**
         * 去添加页面
         */
        $(".addBtn").click(function () {
            $("#content-body").load("news/goAddOrEdit");
        })



        /**
         *  商品表格工具栏
         */
        table.on('tool(news)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if(layEvent === 'view'){//查看详情
                if(data.content!=null){
                    layer.open({
                        type: 1,
                        title: '新闻详情内容',
                        area: ['800px','700px'],
                        content: data.content
                    });
                }else{
                    layer.msg("暂无详情");
                }
            } else if(layEvent === 'delete'){ //删除
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        type: "GET",
                        url: "news/delete",
                        data: {
                            id:data.id,
                            isDelete:0
                        },
                        dataType: "json",
                        success: function(data){
                            layer.msg(data.msg);
                        }
                    });
                });
            } else if(layEvent === 'edit'){ //编辑
                $.ajax({
                    type: "GET",
                    url: "news/goAddOrEdit",
                    data: {
                        id:data.id
                    },
                    success: function(data){
                        $("#content-body").html(data);
                    }
                });
            }
        });
    })
</script>

<!--表格工具栏-->
<script type="text/html" id="bar1">
    <a class="layui-btn layui-btn-mini" lay-event="view">查看详情</a>
    <a class="layui-btn layui-btn-mini layui-btn-normal" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-mini layui-btn-danger" lay-event="delete">删除</a>
</script>

<!-- 模板 -->
<script type="text/html" id="createtime">
    {{getLocalTime(d.createtime)}}
</script>
<script type="text/html" id="updatetime">
    {{getLocalTime(d.updatetime)}}
</script>
</body>
</html>